package com.system.server.impl;

import com.system.dao.TeacherDao;
import com.system.dao.UserDao;
import com.system.pojo.*;
import com.system.server.TeacherService;
import com.system.server.UserService;
import exception.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年04月11日12:06
 * @Description: <描述>
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Course> findCourseById(String username) {
        List<Course> lists = teacherDao.selectAll(username);

        return lists;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<SelectedCourseDTO> findSelectedCourse(Integer id) {
        List<SelectedCourseDTO> lists = teacherDao.findSelectedCourse(id);
        return lists;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public SelectedCourseDTO findSelectedCourseStu(Integer courseid, Integer studentid) {
        SelectedCourseDTO stu = teacherDao.selectThisCourseStu(courseid,studentid);
        return stu;
    }

    @Override
    public void modifyMark(CourseVO courseVO) {
        teacherDao.updateMark(courseVO);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Teacher> findTeacherNo() {
        return teacherDao.selectTeacherNo();
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Teacher> findAll() {
        List<Teacher> lists = teacherDao.findAll();
        return lists;
    }

    @Override
    public List<Teacher> findTeacherByName(String findByName) {
        List<Teacher> lists = teacherDao.selectTeacherByName(findByName);
        return lists;
    }

    @Override
    public Boolean checkStudentID(Integer userID) {
        TeacherVO teacherVO = teacherDao.findStudentByID(userID);
        if(teacherVO==null) {
            return true;
        } else {
            return false;
        }
    }
    @Override
    public void addTeacher(Teacher teacher) {
        teacherDao.insertTeacher(teacher);
    }
    @Override
    public TeacherVO findTeacherById(Integer userID) {
        TeacherVO teacher = teacherDao.selectTeacherById(userID);
        return teacher;
    }

    @Override
    public void modifyTeacher(TeacherVO teacherVO) {
        teacherDao.updateTeacher(teacherVO);
    }

    @Override
    public void removeTeacher(Integer userID) throws DeleteTeacherError {
        try {
            teacherDao.deleteTeacher(userID);
        } catch (Exception e) {
            throw new DeleteTeacherError("删除老师失败,有同学已经选中该老师的课程");
        }
    }

    @Override
    public void modifyPassword(String userName, String newPassword, String againPassword) throws passwordDiffError, modifyPasswordError, userLoginNotFound, passwordNotEmptyError {
        System.out.println(StringUtils.isEmpty(newPassword));
        System.out.println(newPassword.equals(againPassword));
        if(StringUtils.isEmpty(newPassword) && StringUtils.isEmpty(againPassword)) {
            throw new passwordNotEmptyError("密码不能为空");
        }
        if(!newPassword.equals(againPassword)) {
            throw new passwordDiffError("输入的两次密码必须相同并且密码不能为空");
        } else {
            //根据用户名查找登录用户
            UserLogin userLogin = userDao.findUserLoginByName(userName);
            System.out.println(userLogin);
            if(userLogin==null) {
                throw new userLoginNotFound("用户不存在");
            }
            try {
                userDao.updatePassword(newPassword,userLogin.getUserID());
            } catch (Exception e) {
                throw new modifyPasswordError("修改密码错误");
            }
        }
    }
}
